package com.supor.aiot.mine.ui.cooking;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baseconfig.base.GlideApp;
import com.android.baseconfig.base.GlideRequest;
import com.android.baseconfig.base.RxBus3;
import com.supor.aiot.common.adapter.VP2FragmentStateAdapter;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.base.BaseMVVMFragment;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.helper.ViewPager2Helper;
import com.supor.aiot.common.ui.view.DotIndicatorTitleView;
import com.supor.aiot.common.utils.NotNullUtils;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.CookingRecordNumBean;
import com.supor.aiot.mine.databinding.ActivityCookingRecordBinding;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;

import java.util.ArrayList;

import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Observer;

import io.reactivex.rxjava3.functions.Consumer;

@Route(path = PathUrl.HomeMine.PATH_MINE_COOKING_RECORD)
public class CookingRecordActivity extends BaseMVVMEnhanceActivity<CookingRecordModel, ActivityCookingRecordBinding> {

    private ArrayList<String> indicatorDatas;
    private String macAdr;

    @Override
    protected void initView() {
        super.initView();
        if (null != getIntent()) {
            macAdr = getIntent().getStringExtra("RECORD_MAC_ADDR");
        }
        bindingView.flPageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initMagicIndicator();
        if (AppAgent.isLogin() && null != GlobalDataCache.userInfo) {

            GlideRequest<Drawable> glideRequests;
            String avaStr = NotNullUtils.getNotNullValue(GlobalDataCache.userInfo.getFaceImage(), "0");
            switch (avaStr) {
                case "0":
                    glideRequests = GlideApp.with(this).load(R.mipmap.ic_default_head);
                    break;
                case "1":
                    glideRequests = GlideApp.with(this).load(R.mipmap.ic_default_head1);
                    break;
                case "2":
                    glideRequests = GlideApp.with(this).load(R.mipmap.ic_default_head2);
                    break;
                case "3":
                    glideRequests = GlideApp.with(this).load(R.mipmap.ic_default_head3);
                    break;
                case "4":
                    glideRequests = GlideApp.with(this).load(R.mipmap.ic_default_head4);
                    break;
                default:
                    glideRequests = GlideApp.with(this).load(avaStr);
                    break;
            }
            glideRequests.into(bindingView.userAva);

        }
    }

    @Override
    protected void initData() {
        super.initData();
        RxBus3.getInstance().register(Constants.Tag.COOKING_EVAL, (Consumer<String>) s -> {
            viewModel.getCookingNum(bindDestoryAutoDispose());
        }, this.getClass());
        viewModel.getCookingNumResult().observe(this, new Observer<Result<CookingRecordNumBean>>() {
            @Override
            public void onChanged(Result<CookingRecordNumBean> cookingRecordNumBeanResult) {
                if (null != cookingRecordNumBeanResult.getData()) {
                    String cookingNum = String.valueOf(cookingRecordNumBeanResult.getData().getPrCount());
                    String commNum = String.valueOf(cookingRecordNumBeanResult.getData().getPjCount());
                    String cookingRatio = cookingRecordNumBeanResult.getData().getResult();

                    if (!TextUtils.isEmpty(cookingRatio)
                            && !TextUtils.isEmpty(commNum)) {
                        SpannableString invitString = new SpannableString("累计烹饪" + NotNullUtils.getNumValueOrDefault(cookingNum, "-") + "次，" + NotNullUtils.getNumValueOrDefault(commNum, "-") + "次评价");
                        invitString.setSpan(new RelativeSizeSpan(1.3f),
                                4, cookingNum.length() + 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                        invitString.setSpan(new RelativeSizeSpan(1.3f),
                                cookingNum.length() + 6, cookingNum.length() + 6 + commNum.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                        bindingView.tvCookingTimes.setText(invitString);
                    }

                    if (!TextUtils.isEmpty(cookingRatio)) {
                        SpannableString radioString = new SpannableString("您的烹饪次数已经超越" + NotNullUtils.getNumPercentValueOrDefault(cookingRatio, "-") + "的用户");
                        radioString.setSpan(new RelativeSizeSpan(1.3f),
                                10, cookingRatio.length() + 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                        bindingView.tvRadio.setText(radioString);
                    }

                }
            }
        });
        viewModel.getCookingNum(bindDestoryAutoDispose());
    }

    private void initMagicIndicator() {
        indicatorDatas = new ArrayList<>();
        indicatorDatas.add("全部");
        indicatorDatas.add("未评价");
        indicatorDatas.add("已评价");
        CommonNavigator commonNavigator = new CommonNavigator(this);
//        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return indicatorDatas == null ? 0 : indicatorDatas.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                DotIndicatorTitleView dotIndicatorTitleView = new DotIndicatorTitleView(context);
                dotIndicatorTitleView.setText(indicatorDatas.get(index));
                dotIndicatorTitleView.setTextSize(17);
                dotIndicatorTitleView.setNormalColor(Color.parseColor("#FF999999"));
                dotIndicatorTitleView.setSelectedColor(Color.parseColor("#FF333333"));

                dotIndicatorTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bindingView.vp2Content.setCurrentItem(index);
                    }
                });
                return dotIndicatorTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });

        ArrayList<BaseMVVMFragment<? extends AndroidViewModel, ? extends ViewDataBinding>> fragments = new ArrayList<>();
        fragments.add(CookingRecordListFragment.newInstance(-1, macAdr));
        fragments.add(CookingRecordListFragment.newInstance(0, macAdr));
        fragments.add(CookingRecordListFragment.newInstance(1, macAdr));
        bindingView.vp2Content.setAdapter(new VP2FragmentStateAdapter<>(this, fragments));
        bindingView.vp2Content.setOffscreenPageLimit(fragments.size());

        bindingView.magicIndicator.setNavigator(commonNavigator);

        ViewPager2Helper.bind(bindingView.magicIndicator, bindingView.vp2Content);
    }
}